import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../provider/person_info.dart';

class singleChoice extends StatefulWidget {
  final List items;
  String selectedValue;
  double width;
  var setvalue;
  singleChoice(
      {super.key,
      this.setvalue,
      required this.items,
      required this.selectedValue,
      this.width = 250});

  @override
  State<StatefulWidget> createState() => _singleChoice();
}

class _singleChoice extends State<singleChoice> {
  @override
  Widget build(BuildContext context) {
    return Consumer<PersonInfo>(builder: (ctx, person, child) {
      return DropdownButtonHideUnderline(
        child: DropdownButton2(
          isExpanded: true,
          dropdownStyleData: const DropdownStyleData(maxHeight: 200),
          hint: Center(
            child: Text('请选择',
                style: TextStyle(
                  fontSize: 18,
                  color: Theme.of(context).hintColor,
                )),
          ),
          items: widget.items
              .map((item) => DropdownMenuItem<String>(
                    value: item,
                    child: Center(
                      child: Text(item,
                          style: const TextStyle(
                            fontSize: 14,
                          )),
                    ),
                  ))
              .toList(),
          value: widget.selectedValue,
          onChanged: (value) {
            if (widget.setvalue != null) {
              setState(() {
                widget.selectedValue = value as String;
              });
              widget.setvalue(person, value as String);
            } else {
              setState(() {
                widget.selectedValue = value as String;
              });
            }
          },
          buttonStyleData: ButtonStyleData(
              height: 40,
              width: widget.width,
              decoration: BoxDecoration(color: Colors.white),
              padding: EdgeInsets.only(right: 10, left: 10)),
          menuItemStyleData: const MenuItemStyleData(
            height: 40,
          ),
        ),
      );
    });
  }
}
